// 前端与Electron主进程通信工具（仅Electron下可用）
export function sendToMain(channel, data) {
  if (window && window.require) {
    const { ipcRenderer } = window.require('electron')
    ipcRenderer.send(channel, data)
  }
}

export function onFromMain(channel, callback) {
  if (window && window.require) {
    const { ipcRenderer } = window.require('electron')
    ipcRenderer.on(channel, (event, data) => callback(data))
  }
}

export function openSubWindow(url, title = '子窗口') {
  if (window && window.require) {
    const { ipcRenderer } = window.require('electron')
    ipcRenderer.send('open-subwindow', { url, title })
  }
}
